package org.jeecg.modules.qyxx.handler;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.dzswj.vo.response.cwbb.GsCwbbVo;
import org.jeecg.modules.fk.entity.DjOrg;
import org.jeecg.modules.fk.service.IDjOrgService;
import org.jeecg.modules.qyxx.entity.*;
import org.jeecg.modules.qyxx.mapper.TycLsgsxxZchlbMapper;
import org.jeecg.modules.qyxx.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.List;


@Slf4j
@Component
public class SyncTYCHandler {
    @Autowired
    private IDjOrgService djOrgService;

    @Autowired
    private ITycQyjbxxService tycQyjbxxService;

    @Autowired
    private ITycDwtzService tycDwtzService;

    @Autowired
    private ITycLsdwtzService tycLsdwtzService;

    @Autowired
    private ITycLsgsxxDjglbService tycLsgsxxDjglbService;

    @Autowired
    private ITycLsgsxxLsfrlbService tycLsgsxxLsfrlbService;

    @Autowired
    private ITycLsgsxxLsmcService tycLsgsxxLsmcService;

    @Autowired
    private ITycLsgsxxSyfwService tycLsgsxxSyfwService;

    @Autowired
    private ITycLsgsxxZcdzlbService tycLsgsxxZcdzlbService;

    @Autowired
    private ITycLsgsxxZczblbService tycLsgsxxZczblbService;

    @Autowired
    private ITycLsgsxxFzjgmclbService tycLsgsxxFzjgmclbService;

    @Autowired
    private ITycGszzService tycGszzService;

    @Autowired
    private ITycLsgsxxGslxlbService tycLsgsxxGslxlbService;

    @Autowired
    private ITycLsgsxxJyqxlbService tycLsgsxxJyqxlbService;

    @Autowired
    private ITycLsgsxxTyshxydmlbService tycLsgsxxTyshxydmlbService;

    @Autowired
    private ITycLsgsxxZchlbService tycLsgsxxZchlbService;

    @Autowired
    private ITycLszyryLsdjgService tycLszyryLsdjgService;

    @Autowired
    private ITycLszyryLsfrService tycLszyryLsfrService;

    @Autowired
    private ITycLszyryLsgdService tycLszyryLsgdService;

    @Autowired
    private ITycQyjbxxGmjjxyflService qyjbxxGmjjxyflService;

    @Autowired
    private ITycSwpjService tycSwpjService;

    @Autowired
    private ITycSswfService tycSswfService;

    @Autowired
    private RestTemplate restTemplate;

    @XxlJob(value = "syncTYCQyjbxxJob")
    public ReturnT<String> syncTYCQyjbxxJob(String s) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "7600f082-f44d-4472-9d90-4319824c1587");
        HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
        String url = "";
        try {
            List<DjOrg> list = djOrgService.list();
            if (list == null || list.size() <= 0) {
                return ReturnT.FAIL;
            }
            int i = 0;
            for (DjOrg djOrg : list) {
                url = "http://open.api.tianyancha.com/services/open/ic/baseinfo/normal?keyword=%s";
                if (StringUtils.hasLength(djOrg.getShxydm())) {
                    url = String.format(url, djOrg.getShxydm());
                } else {
                    url = String.format(url, djOrg.getNsrmc());
                }
                ResponseEntity<JSONObject> data = restTemplate.exchange(url, HttpMethod.GET, requestEntity, JSONObject.class);
                if (200 == data.getStatusCodeValue()) {
                    if (data.getBody() != null && data.getBody().get("error_code") != null && data.getBody().getInteger("error_code") == 0) {
                        TycQyjbxx tycQyjbxx = data.getBody().getObject("result", TycQyjbxx.class);
                        Date now = new Date();
                        tycQyjbxx.setCreateTime(now);
                        tycQyjbxx.setUpdateTime(now);
                        tycQyjbxxService.saveOrUpdate(tycQyjbxx);
                        //
                        TycQyjbxxGmjjxyfl industryAll = tycQyjbxx.getIndustryAll();
                        industryAll.setShxydm(djOrg.getShxydm());
                        industryAll.setOrgName(djOrg.getNsrmc());
                        qyjbxxGmjjxyflService.save(industryAll);
                    }
                }
                i++;
                if (i == 1000) {
                    i = 0;
                    Thread.sleep(60 * 1000);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        /*if (flag) {
            // TODO 需要更新结果表
        }*/
        return ReturnT.SUCCESS;
    }

    @XxlJob(value = "syncTYCSswfJob")
    public ReturnT<String> syncTYCSswfJob(String s) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "7600f082-f44d-4472-9d90-4319824c1587");
        HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
        String url = "";
        try {
            List<DjOrg> list = djOrgService.list();
            if (list == null || list.size() <= 0) {
                return ReturnT.FAIL;
            }
            int i = 0;
            for (DjOrg djOrg : list) {
                url = "http://open.api.tianyancha.com/services/open/mr/taxContravention/2.0?pageSize=20&keyword=%s&pageNum=1";
                if (StringUtils.hasLength(djOrg.getShxydm())) {
                    url = String.format(url, djOrg.getShxydm());
                } else {
                    url = String.format(url, djOrg.getNsrmc());
                }
                ResponseEntity<JSONObject> data = restTemplate.exchange(url, HttpMethod.GET, requestEntity, JSONObject.class);
                if (200 == data.getStatusCodeValue()) {
                    if (data.getBody() != null && data.getBody().get("error_code") != null && data.getBody().getInteger("error_code") == 0) {
                        List<TycSswf> tycSswfs = data.getBody().getJSONObject("result").getJSONArray("items").toJavaList(TycSswf.class);
                        if (tycSswfs != null && tycSswfs.size() > 0) {
                            for (TycSswf tycSswf : tycSswfs) {
                                tycSswf.setShxydm(djOrg.getShxydm());
                                tycSswfService.saveOrUpdateByMultiId(tycSswf);
                            }
                        }
                    }
                }
                i++;
                if (i == 1000) {
                    i = 0;
                    Thread.sleep(60 * 1000);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        /*if (flag) {
            // TODO 需要更新结果表
        }*/
        return ReturnT.SUCCESS;
    }

    @Autowired
    private ITycQsggService tycQsggService;

    @XxlJob(value = "syncTYCQsggJob")
    public ReturnT<String> syncTYCQsggJob(String s) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "7600f082-f44d-4472-9d90-4319824c1587");
        HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
        String url = "";
        try {
            List<DjOrg> list = djOrgService.list();
            if (list == null || list.size() <= 0) {
                return ReturnT.FAIL;
            }
            int i = 0;
            for (DjOrg djOrg : list) {
                url = "http://open.api.tianyancha.com/services/open/mr/ownTax/2.0?pageSize=20&keyword=%s&pageNum=1";
                if (StringUtils.hasLength(djOrg.getShxydm())) {
                    url = String.format(url, djOrg.getShxydm());
                } else {
                    url = String.format(url, djOrg.getNsrmc());
                }
                ResponseEntity<JSONObject> data = restTemplate.exchange(url, HttpMethod.GET, requestEntity, JSONObject.class);
                if (200 == data.getStatusCodeValue()) {
                    if (data.getBody() != null && data.getBody().get("error_code") != null && data.getBody().getInteger("error_code") == 0) {
                        List<TycQsgg> tycQsggs = data.getBody().getJSONObject("result").getJSONArray("items").toJavaList(TycQsgg.class);
                        if (tycQsggs != null && tycQsggs.size() > 0) {
                            for (TycQsgg tycQsgg : tycQsggs) {
                                tycQsggService.saveOrUpdateByMultiId(tycQsgg);
                            }
                        }
                    }
                }
                i++;
                if (i == 1000) {
                    i = 0;
                    Thread.sleep(60 * 1000);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        /*if (flag) {
            // TODO 需要更新结果表
        }*/
        return ReturnT.SUCCESS;
    }

    @Autowired
    private ITycXzcfService tycXzcfService;

    @XxlJob(value = "syncTYCXzcfJob")
    public ReturnT<String> syncTYCXzcfJob(String s) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "7600f082-f44d-4472-9d90-4319824c1587");
        HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
        String url = "";
        try {
            List<DjOrg> list = djOrgService.list();
            if (list == null || list.size() <= 0) {
                return ReturnT.FAIL;
            }
            int i = 0;
            for (DjOrg djOrg : list) {
                url = "http://open.api.tianyancha.com/services/open/mr/punishmentInfo/3.0?pageSize=20&keyword=%s&pageNum=1";
                if (StringUtils.hasLength(djOrg.getShxydm())) {
                    url = String.format(url, djOrg.getShxydm());
                } else {
                    url = String.format(url, djOrg.getNsrmc());
                }
                ResponseEntity<JSONObject> data = restTemplate.exchange(url, HttpMethod.GET, requestEntity, JSONObject.class);
                if (200 == data.getStatusCodeValue()) {
                    if (data.getBody() != null && data.getBody().get("error_code") != null && data.getBody().getInteger("error_code") == 0) {
                        List<TycXzcf> tycXzcfs = data.getBody().getJSONObject("result").getJSONArray("items").toJavaList(TycXzcf.class);
                        if (tycXzcfs != null && tycXzcfs.size() > 0) {
                            for (TycXzcf tycXzcf : tycXzcfs) {
                                tycXzcf.setShxydm(djOrg.getShxydm());
                                tycXzcf.setOrgName(djOrg.getNsrmc());
                                tycXzcfService.saveOrUpdateByMultiId(tycXzcf);
                            }
                        }
                    }
                }
                i++;
                if (i == 1000) {
                    i = 0;
                    Thread.sleep(60 * 1000);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        /*if (flag) {
            // TODO 需要更新结果表
        }*/
        return ReturnT.SUCCESS;
    }

    @XxlJob(value = "syncTYCDwtzJob")
    public ReturnT<String> syncTYCDwtzJob(String s) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "7600f082-f44d-4472-9d90-4319824c1587");
        HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
        String url = "";
        try {
            List<DjOrg> list = djOrgService.list();
            if (list == null || list.size() <= 0) {
                return ReturnT.FAIL;
            }
            int i = 0;
            for (DjOrg djOrg : list) {
                url = "http://open.api.tianyancha.com/services/open/ic/inverst/2.0?pageSize=20&keyword=%s&pageNum=1";
                if (StringUtils.hasLength(djOrg.getShxydm())) {
                    url = String.format(url, djOrg.getShxydm());
                } else {
                    url = String.format(url, djOrg.getNsrmc());
                }
                ResponseEntity<JSONObject> data = restTemplate.exchange(url, HttpMethod.GET, requestEntity, JSONObject.class);
                if (200 == data.getStatusCodeValue()) {
                    if (data.getBody() != null && data.getBody().get("error_code") != null && data.getBody().getInteger("error_code") == 0) {
                        List<TycDwtz> tycDwtzs = data.getBody().getJSONObject("result").getJSONArray("items").toJavaList(TycDwtz.class);
                        if (tycDwtzs != null && tycDwtzs.size() > 0) {
                            for (TycDwtz tycDwtz : tycDwtzs) {
                                TycDwtz dwtz = tycDwtzService.getById(tycDwtz.getId());
                                if (dwtz == null) {
                                    Date now = new Date();
                                    tycDwtz.setCreateTime(now);
                                    tycDwtz.setUpdateTime(now);
                                    tycDwtz.setShxydm(djOrg.getShxydm());
                                    tycDwtz.setOrgName(djOrg.getNsrmc());
                                    tycDwtzService.saveOrUpdate(tycDwtz);
                                }
                            }
                        }
                    }
                }
                i++;
                if (i == 1000) {
                    i = 0;
                    Thread.sleep(60 * 1000);
                }
            }
        /*if (flag) {
                    System.out.println(tycQyjbxx);
                    // TODO 需要更新结果表
                }*/
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return ReturnT.SUCCESS;
    }

    @XxlJob(value = "syncTYCSwpjJob")
    public ReturnT<String> syncTYCSwpjJob(String s) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "7600f082-f44d-4472-9d90-4319824c1587");
        HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
        String url = "";
        try {
            List<DjOrg> list = djOrgService.list();
            if (list == null || list.size() <= 0) {
                return ReturnT.FAIL;
            }
            int i = 0;
            for (DjOrg djOrg : list) {
                url = "http://open.api.tianyancha.com/services/open/m/taxCredit/2.0?keyword=%s&pageSize=%s&pageNum=%s";
                if (StringUtils.hasLength(djOrg.getShxydm())) {
                    url = String.format(url, djOrg.getShxydm(), 20, 1);
                } else {
                    url = String.format(url, djOrg.getNsrmc(), 20, 1);
                }
                ResponseEntity<JSONObject> data = restTemplate.exchange(url, HttpMethod.GET, requestEntity, JSONObject.class);
                if (200 == data.getStatusCodeValue()) {
                    if (data.getBody() != null && data.getBody().get("error_code") != null && data.getBody().getInteger("error_code") == 0) {
                        List<TycSwpj> tycSwpjs = data.getBody().getJSONObject("result").getJSONArray("items").toJavaList(TycSwpj.class);
                        if (tycSwpjs != null && tycSwpjs.size() > 0) {
                            for (TycSwpj tycSwpj : tycSwpjs) {
                                tycSwpj.setShxydm(djOrg.getShxydm());
                                tycSwpj.setOrgName(djOrg.getNsrmc());
                                Date now = new Date();
                                tycSwpj.setCreateTime(now);
                                tycSwpj.setUpdateTime(now);
                                tycSwpjService.saveOrUpdateByMultiId(tycSwpj);
                            }
                        }
                /*if (flag) {
                    System.out.println(tycQyjbxx);
                    // TODO 需要更新结果表
                }*/
                    }
                }
                i++;
                if (i == 1000) {
                    i = 0;
                    Thread.sleep(60 * 1000);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return ReturnT.SUCCESS;
    }

    @XxlJob(value = "syncTYCLsdwtzJob")
    public ReturnT<String> syncTYCLsdwtzJob(String s) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "7600f082-f44d-4472-9d90-4319824c1587");
        HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
        String url = "";
        try {
            List<DjOrg> list = djOrgService.list();
            if (list == null || list.size() <= 0) {
                return ReturnT.FAIL;
            }
            int i = 0;
            for (DjOrg djOrg : list) {
                url = "http://open.api.tianyancha.com/services/open/hi/invest/2.0?keyword=%s&pageSize=%s&pageNum=%s";
                if (StringUtils.hasLength(djOrg.getShxydm())) {
                    url = String.format(url, djOrg.getShxydm(), 20, 1);
                } else {
                    url = String.format(url, djOrg.getNsrmc(), 20, 1);
                }
                ResponseEntity<JSONObject> data = restTemplate.exchange(url, HttpMethod.GET, requestEntity, JSONObject.class);
                if (200 == data.getStatusCodeValue()) {
                    if (data.getBody() != null && data.getBody().get("error_code") != null && data.getBody().getInteger("error_code") == 0) {
                        List<TycLsdwtz> tycLsdwtzs = data.getBody().getJSONObject("result").getJSONArray("items").toJavaList(TycLsdwtz.class);
                        if (tycLsdwtzs != null && tycLsdwtzs.size() > 0) {
                            for (TycLsdwtz tycLsdwtz : tycLsdwtzs) {
                                tycLsdwtz.setShxydm(djOrg.getShxydm());
                                tycLsdwtz.setOrgName(djOrg.getNsrmc());
                                Date now = new Date();
                                tycLsdwtz.setCreateTime(now);
                                tycLsdwtz.setUpdateTime(now);
                                tycLsdwtzService.saveOrUpdate(tycLsdwtz);
                            }
                        }
                /*if (flag) {
                    System.out.println(tycQyjbxx);
                    // TODO 需要更新结果表
                }*/
                    }
                }
                i++;
                if (i == 1000) {
                    i = 0;
                    Thread.sleep(60 * 1000);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return ReturnT.SUCCESS;
    }

    @XxlJob(value = "syncTYCGszzJob")
    public ReturnT<String> syncTYCGszzJob(String s) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "7600f082-f44d-4472-9d90-4319824c1587");
        HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
        String url = "";
        try {
            List<DjOrg> list = djOrgService.list();
            if (list == null || list.size() <= 0) {
                return ReturnT.FAIL;
            }
            int i = 0;
            for (DjOrg djOrg : list) {
                url = "http://open.api.tianyancha.com/services/open/oi/businessEvent/2.0?name=%s&pageSize=%s&pageNum=%s";
                url = String.format(url, djOrg.getNsrmc(), 20, 1);
                ResponseEntity<JSONObject> data = restTemplate.exchange(url, HttpMethod.GET, requestEntity, JSONObject.class);
                if (200 == data.getStatusCodeValue()) {
                    if (data.getBody() != null && data.getBody().get("error_code") != null && data.getBody().getInteger("error_code") == 0) {
                        List<TycGszz> tycGszzes = data.getBody().getJSONObject("result").getJSONArray("items").toJavaList(TycGszz.class);
                        if (tycGszzes != null && tycGszzes.size() > 0) {
                            for (TycGszz tycGszz : tycGszzes) {
                                Date now = new Date();
                                tycGszz.setShxydm(djOrg.getShxydm());
                                tycGszz.setOrgName(djOrg.getNsrmc());
                                tycGszz.setCreateTime(now);
                                tycGszz.setUpdateTime(now);
                                tycGszzService.saveOrUpdate(tycGszz);
                            }
                        }
                    }
                /*if (flag) {
                    System.out.println(tycQyjbxx);
                    // TODO 需要更新结果表
                }*/
                }
                i++;
                if (i == 1000) {
                    i = 0;
                    Thread.sleep(60 * 1000);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return ReturnT.SUCCESS;
    }

    @XxlJob(value = "syncTYCLszyryJob")
    public ReturnT<String> syncTYCLszyryJob(String s) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "7600f082-f44d-4472-9d90-4319824c1587");
        HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
        String url = "";
        try {
            List<DjOrg> list = djOrgService.list();
            if (list == null || list.size() <= 0) {
                return ReturnT.FAIL;
            }
            int i = 0;
            for (DjOrg djOrg : list) {
                url = "http://open.api.tianyancha.com/services/open/hi/members?keyword=%s";
                if (StringUtils.hasLength(djOrg.getShxydm())) {
                    url = String.format(url, djOrg.getShxydm(), 20, 1);
                } else {
                    url = String.format(url, djOrg.getNsrmc(), 20, 1);
                }
                ResponseEntity<JSONObject> data = restTemplate.exchange(url, HttpMethod.GET, requestEntity, JSONObject.class);
                if (200 == data.getStatusCodeValue()) {
                    if (data.getBody() != null && data.getBody().get("error_code") != null && data.getBody().getInteger("error_code") == 0) {
                        JSONObject result = data.getBody().getJSONObject("result");
                        Date now = new Date();
                        List<List<TycLszyryLsdjg>> tycLszyryLsdjgs = JSONObject.parseObject(result.getJSONArray("pastStafferList").toJSONString(), new TypeReference<List<List<TycLszyryLsdjg>>>() {
                        });
                        // List<TycLszyryLsdjg> tycLszyryLsdjgs = result.getJSONArray("pastStafferList").toJavaList(TycLszyryLsdjg.class);
                        if (tycLszyryLsdjgs != null && tycLszyryLsdjgs.size() > 0) {
                            for (List<TycLszyryLsdjg> tycLszyryLsdjg : tycLszyryLsdjgs) {
                                if (tycLszyryLsdjg != null && tycLszyryLsdjg.size() > 0) {
                                    for (TycLszyryLsdjg lszyryLsdjg : tycLszyryLsdjg) {
                                        lszyryLsdjg.setShxydm(djOrg.getShxydm());
                                        lszyryLsdjg.setOrgName(djOrg.getNsrmc());
                                        lszyryLsdjg.setCreateTime(now);
                                        lszyryLsdjg.setUpdateTime(now);
                                        tycLszyryLsdjgService.saveOrUpdate(lszyryLsdjg);
                                    }
                                }
                            }
                        }
                        // GsCwbbVo gsCwbbVo = JSONObject.parseObject(cwbbJson.toString(), new TypeReference<GsCwbbVo>() {});

                        List<TycLszyryLsfr> tycLszyryLsfrs = result.getJSONArray("pastLegalPersonList").toJavaList(TycLszyryLsfr.class);
                        if (tycLszyryLsfrs != null && tycLszyryLsfrs.size() > 0) {
                            for (TycLszyryLsfr tycLszyryLsfr : tycLszyryLsfrs) {
                                tycLszyryLsfr.setShxydm(djOrg.getShxydm());
                                tycLszyryLsfr.setOrgName(djOrg.getNsrmc());
                                tycLszyryLsfr.setCreateTime(now);
                                tycLszyryLsfr.setUpdateTime(now);
                                tycLszyryLsfrService.saveOrUpdate(tycLszyryLsfr);
                            }
                        }
                        List<TycLszyryLsgd> tycLszyryLsgds = result.getJSONArray("pastholderList").toJavaList(TycLszyryLsgd.class);
                        if (tycLszyryLsgds != null && tycLszyryLsgds.size() > 0) {
                            for (TycLszyryLsgd tycLszyryLsgd : tycLszyryLsgds) {
                                tycLszyryLsgd.setShxydm(djOrg.getShxydm());
                                tycLszyryLsgd.setOrgName(djOrg.getNsrmc());
                                tycLszyryLsgd.setCreateTime(now);
                                tycLszyryLsgd.setUpdateTime(now);
                                tycLszyryLsgdService.saveOrUpdate(tycLszyryLsgd);
                            }
                        }
                    }
                }
             /*if (flag) {
                    System.out.println(tycQyjbxx);
                    // TODO 需要更新结果表
                }*/
                i++;
                if (i == 1000) {
                    i = 0;
                    Thread.sleep(60 * 1000);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return ReturnT.SUCCESS;
    }

    @XxlJob(value = "syncTYCLsgsxxJob")
    public ReturnT<String> syncTYCLsgsxxJob(String s) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "7600f082-f44d-4472-9d90-4319824c1587");
        HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
        String url = "";
        try {
            List<DjOrg> list = djOrgService.list();
            if (list == null || list.size() <= 0) {
                return ReturnT.FAIL;
            }
            int i = 0;
            for (DjOrg djOrg : list) {
                url = "http://open.api.tianyancha.com/services/open/hi/ic/2.0?keyword=%s";
                if (StringUtils.hasLength(djOrg.getShxydm())) {
                    url = String.format(url, djOrg.getShxydm());
                } else {
                    url = String.format(url, djOrg.getNsrmc());
                }
                ResponseEntity<JSONObject> data = restTemplate.exchange(url, HttpMethod.GET, requestEntity, JSONObject.class);
                if (200 == data.getStatusCodeValue()) {
                    if (data.getBody() != null && data.getBody().get("error_code") != null && data.getBody().getInteger("error_code") == 0) {
                        Date now = new Date();
                        JSONObject jsonObject = data.getBody().getJSONObject("result");
                        if (jsonObject != null && jsonObject.get("pastStafferList") != null) {
                            List<List<TycLsgsxxDjglb>> tycLsgsxxDjglbs = JSONObject.parseObject(jsonObject.getJSONArray("pastStafferList").toJSONString(), new TypeReference<List<List<TycLsgsxxDjglb>>>() {
                            });
                            // List<TycLsgsxxDjglb> tycLsgsxxDjglbs = jsonObject.getJSONArray("pastStafferList").toJavaList(TycLsgsxxDjglb.class);
                            if (tycLsgsxxDjglbs != null && tycLsgsxxDjglbs.size() > 0) {
                                for (List<TycLsgsxxDjglb> tycLsgsxxDjglb : tycLsgsxxDjglbs) {
                                    if (tycLsgsxxDjglb != null && tycLsgsxxDjglb.size() > 0) {
                                        for (TycLsgsxxDjglb lsgsxxDjglb : tycLsgsxxDjglb) {
                                            lsgsxxDjglb.setShxydm(djOrg.getShxydm());
                                            lsgsxxDjglb.setOrgName(djOrg.getNsrmc());
                                            lsgsxxDjglb.setCreateTime(now);
                                            lsgsxxDjglb.setUpdateTime(now);
                                            tycLsgsxxDjglbService.saveOrUpdateByMultiId(lsgsxxDjglb);
                                        }
                                    }
                                }
                            }
                        }
                        if (jsonObject != null && jsonObject.get("pastLegalPersonList") != null) {
                            List<TycLsgsxxLsfrlb> tycLsgsxxLsfrlbs = jsonObject.getJSONArray("pastLegalPersonList").toJavaList(TycLsgsxxLsfrlb.class);
                            if (tycLsgsxxLsfrlbs != null && tycLsgsxxLsfrlbs.size() > 0) {
                                for (TycLsgsxxLsfrlb tycLsgsxxLsfrlb : tycLsgsxxLsfrlbs) {
                                    tycLsgsxxLsfrlb.setShxydm(djOrg.getShxydm());
                                    tycLsgsxxLsfrlb.setOrgName(djOrg.getNsrmc());
                                    tycLsgsxxLsfrlb.setCreateTime(now);
                                    tycLsgsxxLsfrlb.setUpdateTime(now);
                                    tycLsgsxxLsfrlbService.saveOrUpdateByMultiId(tycLsgsxxLsfrlb);
                                }
                            }
                        }
                        if (jsonObject != null && jsonObject.get("historyNameList") != null) {
                            List<TycLsgsxxLsmc> tycLsgsxxLsmcs = jsonObject.getJSONArray("historyNameList").toJavaList(TycLsgsxxLsmc.class);
                            if (tycLsgsxxLsmcs != null && tycLsgsxxLsmcs.size() > 0) {
                                for (TycLsgsxxLsmc tycLsgsxxLsmc : tycLsgsxxLsmcs) {
                                    tycLsgsxxLsmc.setShxydm(djOrg.getShxydm());
                                    tycLsgsxxLsmc.setOrgName(djOrg.getNsrmc());
                                    tycLsgsxxLsmc.setCreateTime(now);
                                    tycLsgsxxLsmc.setUpdateTime(now);
                                    tycLsgsxxLsmcService.saveOrUpdateByMultiId(tycLsgsxxLsmc);
                                }
                            }
                        }
                        if (jsonObject != null && jsonObject.get("businessScopeList") != null) {
                            List<TycLsgsxxSyfw> tycLsgsxxSyfws = jsonObject.getJSONArray("businessScopeList").toJavaList(TycLsgsxxSyfw.class);
                            if (tycLsgsxxSyfws != null && tycLsgsxxSyfws.size() > 0) {
                                for (TycLsgsxxSyfw tycLsgsxxSyfw : tycLsgsxxSyfws) {
                                    tycLsgsxxSyfw.setShxydm(djOrg.getShxydm());
                                    tycLsgsxxSyfw.setOrgName(djOrg.getNsrmc());
                                    tycLsgsxxSyfw.setCreateTime(now);
                                    tycLsgsxxSyfw.setUpdateTime(now);
                                    tycLsgsxxSyfwService.saveOrUpdateByMultiId(tycLsgsxxSyfw);
                                }
                            }
                        }
                        if (jsonObject != null && jsonObject.get("locationList") != null) {
                            List<TycLsgsxxZcdzlb> tycLsgsxxZcdzlbs = jsonObject.getJSONArray("locationList").toJavaList(TycLsgsxxZcdzlb.class);
                            if (tycLsgsxxZcdzlbs != null && tycLsgsxxZcdzlbs.size() > 0) {
                                for (TycLsgsxxZcdzlb tycLsgsxxZcdzlb : tycLsgsxxZcdzlbs) {
                                    tycLsgsxxZcdzlb.setShxydm(djOrg.getShxydm());
                                    tycLsgsxxZcdzlb.setOrgName(djOrg.getNsrmc());
                                    tycLsgsxxZcdzlb.setCreateTime(now);
                                    tycLsgsxxZcdzlb.setUpdateTime(now);
                                    tycLsgsxxZcdzlbService.saveOrUpdateByMultiId(tycLsgsxxZcdzlb);
                                }
                            }
                        }
                        if (jsonObject != null && jsonObject.get("regCapitalList") != null) {
                            List<TycLsgsxxZczblb> tycLsgsxxZczblbs = jsonObject.getJSONArray("regCapitalList").toJavaList(TycLsgsxxZczblb.class);
                            if (tycLsgsxxZczblbs != null && tycLsgsxxZczblbs.size() > 0) {
                                for (TycLsgsxxZczblb tycLsgsxxZczblb : tycLsgsxxZczblbs) {
                                    tycLsgsxxZczblb.setShxydm(djOrg.getShxydm());
                                    tycLsgsxxZczblb.setOrgName(djOrg.getNsrmc());
                                    tycLsgsxxZczblb.setCreateTime(now);
                                    tycLsgsxxZczblb.setUpdateTime(now);
                                    tycLsgsxxZczblbService.saveOrUpdateByMultiId(tycLsgsxxZczblb);
                                }
                            }
                        }
                        if (jsonObject != null && jsonObject.get("pastBranchList") != null) {
                            List<TycLsgsxxFzjgmclb> tycLsgsxxFzjgmclbs = jsonObject.getJSONArray("pastBranchList").toJavaList(TycLsgsxxFzjgmclb.class);
                            if (tycLsgsxxFzjgmclbs != null && tycLsgsxxFzjgmclbs.size() > 0) {
                                for (TycLsgsxxFzjgmclb tycLsgsxxFzjgmclb : tycLsgsxxFzjgmclbs) {
                                    tycLsgsxxFzjgmclb.setShxydm(djOrg.getShxydm());
                                    tycLsgsxxFzjgmclb.setOrgName(djOrg.getNsrmc());
                                    tycLsgsxxFzjgmclb.setCreateTime(now);
                                    tycLsgsxxFzjgmclb.setUpdateTime(now);
                                    tycLsgsxxFzjgmclbService.saveOrUpdateByMultiId(tycLsgsxxFzjgmclb);
                                }
                            }
                        }
                        if (jsonObject != null && jsonObject.get("typeList") != null) {
                            List<TycLsgsxxGslxlb> tycLsgsxxGslxlbs = jsonObject.getJSONArray("typeList").toJavaList(TycLsgsxxGslxlb.class);
                            if (tycLsgsxxGslxlbs != null && tycLsgsxxGslxlbs.size() > 0) {
                                for (TycLsgsxxGslxlb tycLsgsxxGslxlb : tycLsgsxxGslxlbs) {
                                    tycLsgsxxGslxlb.setShxydm(djOrg.getShxydm());
                                    tycLsgsxxGslxlb.setOrgName(djOrg.getNsrmc());
                                    tycLsgsxxGslxlb.setCreateTime(now);
                                    tycLsgsxxGslxlb.setUpdateTime(now);
                                    tycLsgsxxGslxlbService.saveOrUpdateByMultiId(tycLsgsxxGslxlb);
                                }
                            }
                        }
                        if (jsonObject != null && jsonObject.get("deadLineList") != null) {
                            List<TycLsgsxxJyqxlb> tycLsgsxxJyqxlbs = jsonObject.getJSONArray("deadLineList").toJavaList(TycLsgsxxJyqxlb.class);
                            if (tycLsgsxxJyqxlbs != null && tycLsgsxxJyqxlbs.size() > 0) {
                                for (TycLsgsxxJyqxlb tycLsgsxxJyqxlb : tycLsgsxxJyqxlbs) {
                                    tycLsgsxxJyqxlb.setShxydm(djOrg.getShxydm());
                                    tycLsgsxxJyqxlb.setOrgName(djOrg.getNsrmc());
                                    tycLsgsxxJyqxlb.setCreateTime(now);
                                    tycLsgsxxJyqxlb.setUpdateTime(now);
                                    tycLsgsxxJyqxlbService.saveOrUpdateByMultiId(tycLsgsxxJyqxlb);
                                }
                            }
                        }
                        if (jsonObject != null && jsonObject.get("creditCodeList") != null) {
                            List<TycLsgsxxTyshxydmlb> tycLsgsxxTyshxydmlbs = jsonObject.getJSONArray("creditCodeList").toJavaList(TycLsgsxxTyshxydmlb.class);
                            if (tycLsgsxxTyshxydmlbs != null && tycLsgsxxTyshxydmlbs.size() > 0) {
                                for (TycLsgsxxTyshxydmlb tycLsgsxxTyshxydmlb : tycLsgsxxTyshxydmlbs) {
                                    tycLsgsxxTyshxydmlb.setShxydm(djOrg.getShxydm());
                                    tycLsgsxxTyshxydmlb.setOrgName(djOrg.getNsrmc());
                                    tycLsgsxxTyshxydmlb.setCreateTime(now);
                                    tycLsgsxxTyshxydmlb.setUpdateTime(now);
                                    tycLsgsxxTyshxydmlbService.saveOrUpdateByMultiId(tycLsgsxxTyshxydmlb);
                                }
                            }
                        }
                        if (jsonObject != null && jsonObject.get("regNumberList") != null) {
                            List<TycLsgsxxZchlb> tycLsgsxxZchlbs = jsonObject.getJSONArray("regNumberList").toJavaList(TycLsgsxxZchlb.class);
                            if (tycLsgsxxZchlbs != null && tycLsgsxxZchlbs.size() > 0) {
                                for (TycLsgsxxZchlb tycLsgsxxZchlb : tycLsgsxxZchlbs) {
                                    tycLsgsxxZchlb.setShxydm(djOrg.getShxydm());
                                    tycLsgsxxZchlb.setOrgName(djOrg.getNsrmc());
                                    tycLsgsxxZchlb.setCreateTime(now);
                                    tycLsgsxxZchlb.setUpdateTime(now);
                                    tycLsgsxxZchlbService.saveOrUpdateByMultiId(tycLsgsxxZchlb);
                                }
                            }
                        }
                /*if (flag) {
                    System.out.println(tycQyjbxx);
                    // TODO 需要更新结果表
                }*/
                    }
                }
                i++;
                if (i == 1000) {
                    i = 0;
                    Thread.sleep(60 * 1000);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return ReturnT.SUCCESS;
    }
}
